package com.dfbz.controller;


import com.dfbz.entity.Article;
import com.dfbz.entity.Dept;
import com.dfbz.entity.User;
import com.dfbz.entity.UserFocus;
import com.dfbz.pojo.Result;
import com.dfbz.pojo.StatusCode;
import com.dfbz.service.DeptService;
import com.dfbz.service.UserFocusService;
import com.dfbz.service.UserService;
import com.dfbz.service.impl.FileServiceImpl;
import com.dfbz.service.impl.UserServiceImpl;
import com.dfbz.utils.StrUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.SimpleFormatter;

@RestController
@RequestMapping("userList")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    UserFocusService userFocusService;


    @Autowired
    DeptService deptService;

    @Autowired
    FileServiceImpl fileService;

    /**
     * 详细信息
     *
     * @param userId 用户id
     * @return
     */
    @RequestMapping("detail/{userId}")
    public Result detail(@PathVariable("userId") Object userId) {
        Map<Object, Object> userDetail = userService.findUserInfo(userId);
        List<Dept> deptList = deptService.deptList();
        userDetail.put("deptList",deptList);

        return new Result(true, StatusCode.OK, "找id 相关详单", userDetail);
    }

    /**
     * 关注逻辑
     *
     * @return
     */
    @RequestMapping("follow/{herId}")
    public Result follow(@PathVariable("herId") Integer herId, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("loginUser");

        Integer loginUserId = loginUser.getId();
        Boolean flag = userFocusService.addFollow(loginUserId, herId);
        if (flag) {
            return new Result(true, StatusCode.OK, "关注成功");
        } else {
            return new Result(true, StatusCode.OK, "取消关注");
        }
    }

//    /**
//     * 拿到部门列表
//     *
//     * @return
//     */
//    @RequestMapping("deptList")
//    public Result getDeptList() {
//        List<Dept> deptList = deptService.deptList();
//        return new Result(true, StatusCode.OK, "成功获取", deptList);
//    }

    @RequestMapping(value = "saveDetail/{id}")
    public Result getSaveDetail(@RequestBody Map<String, Object> dbDataMap,@PathVariable(value ="id") Integer id) {
        User user = new User();
        user.setId(id);

        Set<Map.Entry<String, Object>> entries = dbDataMap.entrySet();
        entries.forEach((k) -> {
            switch (k.getKey()){
                case "gender":
                    user.setGender(k.getValue().toString());
                    break;
                case "phone":
                    user.setPhone(k.getValue().toString());
                    break;
                case "login_time":
                    user.setLoginTime(StrUtil.date(k.getValue().toString()));
                    break;
                case "is_secret":
                    user.setIsSecret(k.getValue().toString());
                    break;
                case "name":
                    user.setDeptName(k.getValue().toString());
                    break;
                case "real_name":
                    user.setRealName(k.getValue().toString());
                    break;
                case "pic":
                    user.setPic(k.getValue().toString());
                    break;
                case "age":
                    user.setAge(Integer.parseInt(k.getValue().toString()));
                    break;
                case "register_time":
                    user.setRegisterTime(StrUtil.date(k.getValue().toString()));
                    break;
                default:
                    break;
            }
        });
        userService.updateByPrimaryKeySelective(user);
        userService.deptIdUpdata(id);

        return new Result(true, StatusCode.OK, "更新成功", null);
    }


    /**
     * 上传图片到库   更新loginUser会话
     *
     * @param file    前端传过来的 file对象
     * @param request 请求域
     * @return 上传的文件名、
     */
    @RequestMapping("uploadPic")
    public Result setUploadPic(MultipartFile file, HttpServletRequest request) {
        String fileName = fileService.setUploadPic(file);

        //头像更新的同时  更新会话域
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("loginUser");
        user.setPic(fileName);
        session.setAttribute("loginUser", user);

        //返回文件名做异步更新
        return new Result(true, StatusCode.OK, "图片上传成功", fileName);
    }


    /**
     * 找我关注过 谁
     * @param myId 我的id
     * @return id列表
     */
    @RequestMapping("isFollowHer/{myId}")
    public Result isFollowHer(@PathVariable(value = "myId") String myId){
        List<Integer> followList=userFocusService.isFollowHer(myId);

        return new Result(true,StatusCode.OK,"关注列表id",followList);
    }

    @RequestMapping("pageCondition/{pageNum}/{pageSize}")
    public Result pageConditionShow(@PathVariable ("pageNum") Integer pageNum
            ,@PathVariable("pageSize") Integer pageSize
            ,@RequestBody Map<String,String> params){
        PageInfo<User> dbPageInfo=userService.pageConditionShow(params,pageNum,pageSize);

        return new Result(true,StatusCode.OK,"操作成功",dbPageInfo);
    }



    @RequestMapping("followList/{pageNum}/{pageSize}")
    public Result followList(@PathVariable("pageNum")Integer pageNum
            ,@PathVariable("pageSize") Integer pageSize
            ,@RequestBody Map<String,Integer> map){

        Integer id = map.get("id");
        PageInfo<User> followPageInfo = userService.findFollowPageInfo(pageNum,pageSize,id);
        return new Result(true,StatusCode.OK,"查找列表成功",followPageInfo);
    }

    @RequestMapping("findAllDept")
    public Result findAllDept(){
        List<Map<String,Object>> allDept=userService.findAllDept();

        return new Result(true,StatusCode.OK,"查找部门 列表成功",allDept);
    }

}